package com.frcdi.qms.web.controller.equipment;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.equipment.domain.EquipmentValidation;
import com.frcdi.qms.equipment.service.IEquipmentValidationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备验证记录Controller
 *
 * @author cell
 * @date 2023-12-19
 */
@Api(tags = "设备验证记录")
@RestController
@RequestMapping("/equipment/validation")
public class EquipmentValidationController extends BaseController
{
    @Autowired
    private IEquipmentValidationService equipmentValidationService;

    /**
     * 查询设备验证记录列表
     */
    @ApiOperation("查询设备验证记录列表")
    @PreAuthorize("@ss.hasPermi('equipment:validation:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentValidation equipmentValidation)
    {
        startPage();
        List<EquipmentValidation> list = equipmentValidationService.selectEquipmentValidationList(equipmentValidation);
        return getDataTable(list);
    }

    /**
     * 导出设备验证记录列表
     */
    @ApiOperation("导出设备验证记录列表")
    @PreAuthorize("@ss.hasPermi('equipment:validation:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentValidation equipmentValidation)
    {
        List<EquipmentValidation> list = equipmentValidationService.selectEquipmentValidationList(equipmentValidation);
        ExcelUtil<EquipmentValidation> util = new ExcelUtil<EquipmentValidation>(EquipmentValidation.class);
        util.exportExcel(response, list, "设备验证记录数据");
    }

    /**
     * 获取设备验证记录详细信息
     */
    @ApiOperation("获取设备验证记录详细信息")
    @PreAuthorize("@ss.hasPermi('equipment:validation:query')or @ss.hasPermi('workflow:flowable:list')")
    @GetMapping(value = "/{validationId}")
    public AjaxResult getInfo(@PathVariable("validationId") Long validationId)
    {
        return success(equipmentValidationService.selectEquipmentValidationByValidationId(validationId));
    }

    /**
     * 新增设备验证记录
     */
    @ApiOperation("新增设备验证记录")
    @PreAuthorize("@ss.hasPermi('equipment:validation:add')")
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentValidation equipmentValidation)
    {
        equipmentValidation.setCreateBy(getUsername());
        return toAjax(equipmentValidationService.insertEquipmentValidation(equipmentValidation));
    }

    /**
     * 修改设备验证记录
     */
    @ApiOperation("修改设备验证记录")
    @PreAuthorize("@ss.hasPermi('equipment:validation:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentValidation equipmentValidation)
    {
        equipmentValidation.setUpdateBy(getUsername());
        // 编辑时重置所有状态
        equipmentValidation.setStatus(UserConstants.EXECUTION_NOT);
        equipmentValidation.setAuditResult(UserConstants.AUDIT_NOT_AUDITED);
        return toAjax(equipmentValidationService.updateEquipmentValidation(equipmentValidation));
    }

    /**
     * 修改设备验证记录
     */
    @ApiOperation("修改设备验证记录")
    @PreAuthorize("@ss.hasPermi('equipment:validation:edit')")
    @PutMapping("/validation")
    public AjaxResult editById(@RequestBody EquipmentValidation equipmentValidation)
    {
        equipmentValidation.setUpdateBy(getUsername());
        return toAjax(equipmentValidationService.updateEquipmentValidation(equipmentValidation));
    }

    /**
     * 删除设备验证记录
     */
    @ApiOperation("删除设备验证记录")
    @PreAuthorize("@ss.hasPermi('equipment:validation:remove')")
	@DeleteMapping("/{validationIds}")
    public AjaxResult remove(@PathVariable Long[] validationIds)
    {
        return toAjax(equipmentValidationService.deleteEquipmentValidationByValidationIds(validationIds));
    }

    @ApiOperation("评估设备验证")
    @PreAuthorize("@ss.hasPermi('equipment:validation:edit')")
    @PutMapping("/appraise/{validationId}")
    public AjaxResult appraise(@PathVariable("validationId") Long validationId)
    {
        AjaxResult ajax = AjaxResult.success();
        boolean pass = equipmentValidationService.appraiseEquipmentValidation(validationId);
        return ajax.put("pass", pass);
    }
}
